﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Drawing;

namespace CtripSOA
{
    class XmlOpreate
    {
        XmlDocument textdoc = new XmlDocument();
        
        public void XmlToTree()
        {

        }
  
        public string LoadXml(string xml, string nodeName) {
        	textdoc.LoadXml(xml);
			XmlNode node = textdoc.SelectSingleNode(nodeName);
			MessageBox.Show(node.InnerText.ToString());
			return node.InnerText.ToString();
		}

        /// <summary>
        /// 加载XML文件
        /// </summary>
        /// <param name="xmlFilePath">XML文件路径</param>
        /// <returns></returns>
        public static XmlDocument LoadXmlDoc(string xmlpath)
        {
            var xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlpath);
            return xmlDoc;
        }

        /// 根据指定的XPath表达式获取XML结点列表
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <param name="xpathExpr"></param>
        /// <returns></returns>
        public static XmlNodeList GetXmlNodes(XmlDocument xmlDoc, string xpathExpr)
        {
            if (xmlDoc == null)
                return null;
            return xmlDoc.SelectNodes(xpathExpr);
        }

        public static string GetXmlNodeInfo(XmlNode node, string type = "xml")
        {
            if (node == null)
                return "Empty node or error node";
            string xmlNodeInfo = null;
            switch (type)
            {
                case "text":
                    xmlNodeInfo = node.InnerText;
                    break;
                default:
                    xmlNodeInfo = node.InnerXml;
                    break;
            }
            return xmlNodeInfo;
        }
        
        public void populateTreeControl(System.Xml.XmlNode document, System.Windows.Forms.TreeNodeCollection nodes)
        {
            foreach (System.Xml.XmlNode node in document.ChildNodes)
            {
                // If the element has a value, display it; 
                // otherwise display the first attribute 
                // (if there is one) or the element name 
                // (if there isn't)
                if (node.Name != "FatSendUrl" && node.Name != "UatSendUrl" && node.Name != "ProSendUrl" && node.Name != "FilePath")
                {
                    string text = (node.Value != null ? node.Value :
                     (node.Attributes != null &&
                  node.Attributes.Count > 0) ?
                     node.Attributes[0].Value : node.Name);
                    TreeNode new_child = new TreeNode(text);
                    nodes.Add(new_child);
                    populateTreeControl(node, new_child.Nodes);
                }
            }
        }
        
        public void getTreeByDirectory(System.IO.DirectoryInfo root, System.Windows.Forms.TreeNodeCollection nodes) {
        	System.IO.FileInfo[] files = null;
        	System.IO.DirectoryInfo[] subDirs = null;
        	
        	try {
        		files = root.GetFiles("*.*");
        	} catch(UnauthorizedAccessException e) {
        		MessageBox.Show(e.Message);
        	} catch(System.IO.DirectoryNotFoundException e) {
        		MessageBox.Show(e.Message);
        	}
        	
        	if (files != null) {
        		foreach(System.IO.FileInfo fi in files) {
        			TreeNode new_childFile = new TreeNode(fi.Name);
        			new_childFile.ImageIndex = 1;
        			new_childFile.SelectedImageIndex = 1;
        			nodes.Add(new_childFile);
        		}
        	}
        	
        	subDirs = root.GetDirectories();
        	
        	foreach(System.IO.DirectoryInfo dirInfo in subDirs) {
        		TreeNode new_childDirectory = new TreeNode(dirInfo.Name);
        		new_childDirectory.ImageIndex = 0;
        		nodes.Add(new_childDirectory);
        		getTreeByDirectory(dirInfo, new_childDirectory.Nodes);
        	}
        }
    }
}
